Apache HTTP Client হল একটি শক্তিশালী লাইব্রেরি যা Java অ্যাপ্লিকেশনগুলিতে HTTP প্রোটোকল ব্যবহার করে সার্ভারের সাথে যোগাযোগ স্থাপন করতে ব্যবহৃত হয়। এটি আপনাকে GET, POST, PUT, DELETE সহ বিভিন্ন HTTP মেথড ব্যবহার করে অনুরোধ (request) পাঠাতে সাহায্য করে এবং সার্ভারের থেকে সাড়া (response) গ্রহণ করতে সক্ষম করে।
এই লেখায়, আমরা Apache HTTP Client ব্যবহার করে কিভাবে বিভিন্ন ধরনের HTTP request (যেমন GET, POST, PUT, DELETE) পাঠানো হয় তা দেখব।
GET মেথড ব্যবহার করা হয় সার্ভার থেকে তথ্য রিট্রিভ (retrieve) করতে। এটি সাধারণত ডেটা পড়ার জন্য ব্যবহৃত হয় এবং এতে request body থাকে না, শুধুমাত্র URL এবং হেডার থাকে।
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientGetExample {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// HTTP GET request
HttpGet request = new HttpGet("https://api.example.com/data");
// Send the request and get the response
HttpResponse response = httpClient.execute(request);
// Get the response content as a String
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
এখানে:
POST মেথডটি সাধারণত সার্ভারে নতুন তথ্য পাঠানোর জন্য ব্যবহৃত হয়। POST অনুরোধে request body থাকে, যাতে ক্লায়েন্ট সার্ভারে তথ্য পাঠায় (যেমন JSON, XML, অথবা form data)।
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientPostExample {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// HTTP POST request
HttpPost post = new HttpPost("https://api.example.com/submit");
// Set the body of the POST request
String json = "{\"name\":\"John\", \"age\":30}";
post.setEntity(new StringEntity(json));
post.setHeader("Content-Type", "application/json");
// Send the request and get the response
HttpResponse response = httpClient.execute(post);
// Get the response content as a String
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
এখানে:
PUT মেথডটি সাধারণত একটি রিসোর্স (resource) সম্পূর্ণভাবে আপডেট বা প্রতিস্থাপন করতে ব্যবহৃত হয়। PUT অনুরোধে সার্ভারে পূর্ণ বা আংশিক ডেটা পাঠানো হয়, যা প্রক্রিয়া হয়ে সার্ভারে সংরক্ষণ হয়।
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientPutExample {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// HTTP PUT request
HttpPut put = new HttpPut("https://api.example.com/update");
// Set the body of the PUT request
String json = "{\"name\":\"John\", \"age\":35}";
put.setEntity(new StringEntity(json));
put.setHeader("Content-Type", "application/json");
// Send the request and get the response
HttpResponse response = httpClient.execute(put);
// Get the response content as a String
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
এখানে:
DELETE মেথডটি একটি নির্দিষ্ট রিসোর্স বা ডেটা মুছে ফেলতে ব্যবহৃত হয়। DELETE অনুরোধ সাধারণত ওয়েব সার্ভিস বা API এর মধ্যে ডেটা মুছে ফেলার জন্য ব্যবহৃত হয়।
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientDeleteExample {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// HTTP DELETE request
HttpDelete delete = new HttpDelete("https://api.example.com/delete/123");
// Send the request and get the response
HttpResponse response = httpClient.execute(delete);
// Get the response content as a String
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
} catch (Exception e) {
e.printStackTrace();
}
}
}
এখানে:
Apache HTTP Client একটি অত্যন্ত কার্যকরী লাইব্রেরি যা Java অ্যাপ্লিকেশন থেকে HTTP অনুরোধ এবং উত্তর পরিচালনা করতে সাহায্য করে। আপনি এই লাইব্রেরির মাধ্যমে GET, POST, PUT, এবং DELETE মেথডগুলি ব্যবহার করে বিভিন্ন সার্ভিসের সাথে যোগাযোগ করতে পারেন। এটি আধুনিক HTTP মেথডের মাধ্যমে ওয়েব অ্যাপ্লিকেশন এবং RESTful API এর মধ্যে ডেটা আদান প্রদান করার জন্য অত্যন্ত উপযোগী।
Apache HTTP Client ব্যবহার করে HttpGet
দিয়ে একটি HTTP GET অনুরোধ পাঠানোর জন্য নিচে একটি উদাহরণ এবং ব্যাখ্যা দেওয়া হলো।
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ApacheHttpGetExample {
public static void main(String[] args) {
// HttpClient তৈরি করুন
CloseableHttpClient httpClient = HttpClients.createDefault();
// GET অনুরোধের জন্য URL দিন
String url = "https://jsonplaceholder.typicode.com/posts/1";
try {
// HttpGet অবজেক্ট তৈরি করুন
HttpGet httpGet = new HttpGet(url);
System.out.println("Executing request: " + httpGet.getRequestLine());
// অনুরোধ পাঠিয়ে প্রতিক্রিয়া সংগ্রহ করুন
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
// প্রতিক্রিয়া থেকে স্ট্যাটাস কোড পান
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Response Status Code: " + statusCode);
// প্রতিক্রিয়া থেকে HttpEntity পান
HttpEntity entity = response.getEntity();
if (entity != null) {
// প্রতিক্রিয়ার কন্টেন্ট স্ট্রিং আকারে পড়ুন
String responseBody = EntityUtils.toString(entity);
System.out.println("Response Content: " + responseBody);
}
} finally {
// প্রতিক্রিয়া বন্ধ করুন
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// HttpClient বন্ধ করুন
httpClient.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
HttpClient তৈরি করা
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpClients.createDefault()
ব্যবহার করে ডিফল্ট কনফিগারেশনে একটি HTTP ক্লায়েন্ট তৈরি করা হয়েছে।HttpGet তৈরি করা
HttpGet httpGet = new HttpGet(url);
HttpGet
অবজেক্ট দিয়ে GET অনুরোধ প্রস্তুত করা হয়েছে।HttpGet
এর কনস্ট্রাক্টরে পাস করা হয়েছে।অনুরোধ পাঠানো এবং প্রতিক্রিয়া সংগ্রহ
CloseableHttpResponse response = httpClient.execute(httpGet);
httpClient.execute(httpGet)
ব্যবহার করে GET অনুরোধ সার্ভারে পাঠানো হয়েছে।CloseableHttpResponse
অবজেক্টে সংরক্ষণ করা হয়েছে।স্ট্যাটাস কোড পড়া:
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Response Status Code: " + statusCode);
সার্ভার থেকে প্রাপ্ত HTTP স্ট্যাটাস কোড (200
, 404
ইত্যাদি) প্রদর্শন করা হয়েছে।
বডি কন্টেন্ট পড়া:
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
HttpEntity
থেকে প্রতিক্রিয়ার ডেটা EntityUtils.toString()
এর মাধ্যমে স্ট্রিং আকারে পড়া হয়েছে।
প্রতিক্রিয়া বন্ধ করুন:
response.close();
HttpClient বন্ধ করুন:
httpClient.close();
HttpGet
সহজ এবং কার্যকর পদ্ধতি HTTP GET অনুরোধের জন্য।https://jsonplaceholder.typicode.com/posts/1
ব্যবহার করা হয়েছে, যা একটি উন্মুক্ত API।Apache HTTP Client ব্যবহার করে HttpPost
ব্যবহার করে একটি POST request পাঠানোর উদাহরণ নিচে দেওয়া হলো। এটি জাভা প্রোগ্রামিং ভাষায় লেখা।
এই উদাহরণে, আমরা একটি নির্দিষ্ট URL-এ ডেটা পাঠাব এবং সার্ভারের কাছ থেকে রেসপন্স গ্রহণ করব।
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ApacheHttpClientPostExample {
public static void main(String[] args) {
// HTTP ক্লায়েন্ট তৈরি
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// POST Request এর URL
String url = "https://example.com/api";
// HttpPost অবজেক্ট তৈরি
HttpPost postRequest = new HttpPost(url);
// JSON বা অন্য ধরনের ডেটা পাঠানোর জন্য কন্টেন্ট সেট করা
String json = "{ \"name\": \"John\", \"age\": 30 }";
StringEntity entity = new StringEntity(json);
// Request Content-Type সেট করা
postRequest.setEntity(entity);
postRequest.setHeader("Accept", "application/json");
postRequest.setHeader("Content-Type", "application/json");
// POST Request পাঠানো এবং Response গ্রহণ করা
HttpResponse response = httpClient.execute(postRequest);
// Response থেকে স্ট্যাটাস কোড এবং কন্টেন্ট নেওয়া
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Response Code: " + statusCode);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
String responseBody = EntityUtils.toString(responseEntity);
System.out.println("Response Body: " + responseBody);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
HttpPost
ক্লাস:StringEntity
:HttpClients.createDefault()
:Accept
: সার্ভার থেকে কী ধরনের রেসপন্স প্রত্যাশা করছেন।Content-Type
: পাঠানো ডেটার ফরম্যাট।EntityUtils.toString()
:Apache HTTP Client ব্যবহার করার জন্য Maven-এর pom.xml
ফাইলে নিচের dependency যোগ করুন:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
যদি POST request সফল হয়, তাহলে এটি এরকম আউটপুট দিতে পারে:
Response Code: 200
Response Body: { "message": "Data received successfully" }
যদি কোনও অংশে সমস্যা হয় বা আপনাকে নির্দিষ্ট ডেটা হ্যান্ডলিং নিয়ে সাহায্য প্রয়োজন, জানাতে পারেন।
Apache HTTP Client:
HttpPut
এবং HttpDelete
হলো HTTP প্রোটোকলের PUT এবং DELETE মেথডের সাথে সম্পর্কিত। এগুলো সাধারণত RESTful API ব্যবহারের সময় ডেটা আপডেট এবং মুছে ফেলার জন্য ব্যবহৃত হয়। নিচে এই দুটি মেথড ব্যবহার করার পদ্ধতি বর্ণনা করা হলো:
PUT রিকোয়েস্ট সাধারণত সার্ভারে ডেটা আপডেট করতে ব্যবহৃত হয়। উদাহরণস্বরূপ, কোনো REST API-তে একটি নির্দিষ্ট রিসোর্স আপডেট করতে চাইলে HttpPut
ব্যবহার করা হয়।
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpResponse;
public class HttpPutExample {
public static void main(String[] args) {
String url = "https://api.example.com/resource/123"; // যেখানে আপডেট হবে
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPut httpPut = new HttpPut(url);
httpPut.setHeader("Content-Type", "application/json");
// ডেটা সেট করা (JSON format)
String json = "{\"name\":\"Updated Name\", \"age\":30}";
StringEntity entity = new StringEntity(json);
httpPut.setEntity(entity);
// রিকোয়েস্ট পাঠানো
HttpResponse response = httpClient.execute(httpPut);
System.out.println("Response Status: " + response.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
}
}
HttpPut
দিয়ে নির্দিষ্ট URL-এ রিকোয়েস্ট পাঠানো হয়।StringEntity
দিয়ে JSON ডেটা যোগ করা হয়।200
বা 204
হতে পারে।DELETE রিকোয়েস্ট ব্যবহার করে REST API-র নির্দিষ্ট রিসোর্স মুছে ফেলা যায়।
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpResponse;
public class HttpDeleteExample {
public static void main(String[] args) {
String url = "https://api.example.com/resource/123"; // যেখানে ডেটা মুছে ফেলবে
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpDelete httpDelete = new HttpDelete(url);
// রিকোয়েস্ট পাঠানো
HttpResponse response = httpClient.execute(httpDelete);
System.out.println("Response Status: " + response.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
}
}
HttpDelete
ব্যবহার করে নির্দিষ্ট রিসোর্স মুছে ফেলা যায়।200
, 202
, বা 204
হয়।Authorization
, Content-Type
ইত্যাদি হেডার প্রয়োজন হতে পারে।httpPut.setHeader("Authorization", "Bearer your_token_here");
httpDelete.setHeader("Authorization", "Bearer your_token_here");
HttpPut
ডেটা আপডেটের জন্য এবং HttpDelete
ডেটা মুছে ফেলার জন্য অত্যন্ত কার্যকর।অ্যাপাচি এইচটিটিপি ক্লায়েন্ট (Apache HTTP Client)-এর মাধ্যমে URIBuilder
ব্যবহার করে URL কাস্টমাইজ করা একটি সাধারণ কাজ। URIBuilder
একটি ক্লাস যা URL তৈরি ও মডিফাই করার জন্য সহায়ক। এটি সহজে URI এর বিভিন্ন অংশ (যেমন scheme, host, path, query parameters) সেট এবং পরিবর্তন করতে দেয়।
নিচে একটি উদাহরণ দেওয়া হলো যেখানে URIBuilder
ব্যবহার করে URL কাস্টমাইজ করা হয়েছে:
import org.apache.http.client.utils.URIBuilder;
import java.net.URI;
import java.net.URISyntaxException;
public class URICustomizationExample {
public static void main(String[] args) {
try {
// URIBuilder ইনস্ট্যান্স তৈরি
URIBuilder uriBuilder = new URIBuilder();
// Scheme (http/https) সেট করা
uriBuilder.setScheme("https");
// Host (ডোমেইন) সেট করা
uriBuilder.setHost("example.com");
// Path (URI পথ) সেট করা
uriBuilder.setPath("/api/v1/resource");
// Query Parameters যোগ করা
uriBuilder.addParameter("key1", "value1");
uriBuilder.addParameter("key2", "value2");
// URI তৈরি
URI uri = uriBuilder.build();
// ফলাফল প্রিন্ট করা
System.out.println("Customized URI: " + uri.toString());
} catch (URISyntaxException e) {
// যদি URI ভুল ফরম্যাটে হয়, ত্রুটি পরিচালনা
System.err.println("Error building URI: " + e.getMessage());
}
}
}
Customized URI: https://example.com/api/v1/resource?key1=value1&key2=value2
setScheme
: URL এর scheme নির্ধারণ করে (যেমন HTTP বা HTTPS)।setHost
: হোস্ট বা ডোমেইন যোগ করে (যেমন example.com
)।setPath
: URI এর পাথ নির্ধারণ করে।addParameter
: কুইরি প্যারামিটার যোগ করতে ব্যবহৃত হয়।build
: সম্পূর্ণ URI তৈরি করে।এটি জাভা-ভিত্তিক অ্যাপ্লিকেশন ডেভেলপমেন্টে প্রচলিত এবং অত্যন্ত কার্যকর।
common.read_more